#coding=utf-8
from django import template
from django.template.defaultfilters import stringfilter
import datetime
register = template.Library()

from common import time_utils
from common.utils import process_html
import hashlib
from google.appengine.api import users

def make_list(start, end):
    """
        {{ start|make_list:end}}
    """
    return range(start, end)
register.filter('make_list', make_list)

def human_time(target_time):
  return time_utils.human_time(target_time)
register.filter('human_time', human_time)

def format_time(target_time):
  return time_utils.time_str(target_time)
register.filter('format_time', format_time)

def rfc3339_time_str(dt):
    return time_utils.rfc3339_time_str(dt)
register.filter('rfc3339_time_str', rfc3339_time_str)

def date_str(target_time):
  return time_utils.date_str(target_time)
register.filter('date_str', date_str)

def only_time_str(target_time):
  return time_utils.only_time_str(target_time)
register.filter('only_time_str', only_time_str)

def mod(value, divisor = 2):
    """
        {{ value|make_list:divisor}}
    """
    return (value%divisor == 0)
register.filter('mod', mod)

def logout_url(url):
    return '/logout/?url=%s' % url
register.filter('logout_url', logout_url)

def login_url(url):
    return users.create_login_url(url)
register.filter('login_url', login_url)

def process_html_show(html):
    return process_html(html)
register.filter('process_html_show', process_html_show)

def md5(s):
    return hashlib.md5(s).hexdigest()
register.filter('md5', md5)

def cover_space(s, autoescape=None):
    return s.replace(' ', '&nbsp;')
cover_space.is_safe = True
cover_space.needs_autoescape = True
register.filter('cover_space', cover_space)